//
//  TXVisitorView.swift
//  MicroBlog
//
//  Created by Jokin on 2017/3/27.
//  Copyright © 2017年 jokin. All rights reserved.
//

import UIKit
import SnapKit


class TXVisitorView: UIView {

    private lazy var imageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var homeImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    var callBack : (() ->())?
    
    
    
    private lazy var messafeLabel: UILabel = {
        let lebel = UILabel()
        lebel.textColor = UIColor.darkGray
        lebel.font = UIFont.systemFont(ofSize: 13)
        lebel.textAlignment = .center
        lebel.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lebel.numberOfLines = 0
        return lebel
    }()
    private var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.adjustsImageWhenHighlighted  = false
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.addTarget(self, action: #selector(touchLogInAction), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        return btn
    }()
    private var logInBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", for: .normal)
        btn.adjustsImageWhenHighlighted  = false
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.addTarget(self, action: #selector(touchLogInAction), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        return btn
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI()  {
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        addSubview(imageView)
        addSubview(maskIconImageView)
        addSubview(homeImageView)
        addSubview(messafeLabel)
        addSubview(registerBtn)
        addSubview(logInBtn)
        imageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(imageView)
        }
        homeImageView.snp.makeConstraints { (make) in
            make.center.equalTo(maskIconImageView)
        }
        messafeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(homeImageView)
            make.top.equalTo(imageView.snp.bottom)
            make.width.equalTo(224)
        }
        registerBtn.snp.makeConstraints { (make) in
            make.leading.equalTo(messafeLabel)
            make.top.equalTo(messafeLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        logInBtn.snp.makeConstraints { (make) in
            make.trailing.equalTo(messafeLabel)
            make.top.equalTo(registerBtn)
            make.size.equalTo(registerBtn)
        }
        
    }
    
    func starAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.isRemovedOnCompletion = false
        imageView.layer.add(animation, forKey: nil)
        
        
    }
    
    func changeVisitor(imageName: String?, text: String?) {
        if let imageN = imageName,let mas = text {
            homeImageView.image = UIImage(named: imageN)
            messafeLabel.text = mas
            imageView.isHidden = true
        }else {
            starAnimation()
        }
    }
    
    func touchLogInAction() {
        if (callBack != nil){
            callBack?()
        }
        
        
        
    }
    

}



